package com.zjw.touchscreen;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.PermissionChecker;
import com.zjw.touchscreen.listener.lixiang.*;

public class OperateActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback {

    private int permissionCount = 0;

    private LinearLayout linearLayout;

    private ImageView topTouch;

    private ImageView leftTouch;

    private ImageView centerTouch;

    private ImageView rightTouch;

    private ImageView bottomTouch;

    private ImageView homeTouch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operate);
        if (!requestPermissions()) {
            return;
        }
        initView();
    }

    private void initView() {
        linearLayout = findViewById(R.id.remote_control);
        topTouch = findViewById(R.id.top_touch);
        leftTouch = findViewById(R.id.left_touch);
        centerTouch = findViewById(R.id.center_touch);
        rightTouch = findViewById(R.id.right_touch);
        bottomTouch = findViewById(R.id.bottom_touch);
        homeTouch = findViewById(R.id.home_touch);
        initBackground();
        initTopTouch();
        initLeftTouch();
        initCenterTouch();
        initRightTouch();
        initBottomTouch();
        initHomeTouch();
        linearLayout.setOnTouchListener(new ParentTouchBoardListener());
        topTouch.setOnTouchListener(new TopTouchBoardListener());
        leftTouch.setOnTouchListener(new LeftTouchBoardListener());
        centerTouch.setOnTouchListener(new CenterTouchBoardListener());
        rightTouch.setOnTouchListener(new RightTouchBoardListener());
        bottomTouch.setOnTouchListener(new BottomTouchBoardListener());
        homeTouch.setOnTouchListener(new HomeTouchBoardListener());
    }

    private void initHomeTouch() {
        ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) homeTouch.getLayoutParams();
        marginParams.leftMargin = (int)(150 * (width / (double)428));
        marginParams.topMargin = (int) (38 * (height / (double)821));
        marginParams.width = (int)(130 * (width / (double)428));
        marginParams.height = (int) (130 * (height / (double)821));
        homeTouch.setLayoutParams(marginParams);
    }
    private void initBottomTouch() {
        ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) bottomTouch.getLayoutParams();
        marginParams.leftMargin = (int)(30 * (width / (double)428));
        marginParams.topMargin = 0;
        marginParams.width = (int)(370 * (width / (double)428));
        marginParams.height = (int) (50 * (height / (double)821));
        bottomTouch.setLayoutParams(marginParams);
    }

    private void initRightTouch() {
        ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) rightTouch.getLayoutParams();
        marginParams.leftMargin = 0;
        marginParams.topMargin = 0;
        marginParams.width = (int) (50 * (width / (double)428));
        marginParams.height = (int) (int) (430 * (height / (double)821));
        rightTouch.setLayoutParams(marginParams);
    }

    private void initCenterTouch() {
        ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) centerTouch.getLayoutParams();
        marginParams.leftMargin = 0;
        marginParams.topMargin = 0;
        marginParams.width = (int) (270 * (width / (double)428));
        marginParams.height = (int) (int) (430 * (height / (double)821));
        centerTouch.setLayoutParams(marginParams);
    }
    private void initLeftTouch() {
        ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) leftTouch.getLayoutParams();
        marginParams.leftMargin = (int)(30 * (width / (double)428));
        marginParams.topMargin = 0;
        marginParams.width = (int) (50 * (width / (double)428));
        marginParams.height = (int) (430 * (height / (double)821));
        leftTouch.setLayoutParams(marginParams);
    }

    private void initTopTouch() {
        ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) topTouch.getLayoutParams();
        marginParams.leftMargin = (int)(30 * (width / (double)428));
        marginParams.topMargin = (int) (50 * (height / (double)821));
        marginParams.width = (int)(370 * (width / (double)428));
        marginParams.height = (int) (50 * (height / (double)821));
        topTouch.setLayoutParams(marginParams);
    }

    private void initBackground() {
        //屏幕尺寸
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;
        // 获取Drawable图片的尺寸
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.touch_board);
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        //计算imageView宽高
        int width = screenWidth;
        int height = (int) (width * (intrinsicHeight / (float) intrinsicWidth));
        if (height > screenHeight) {
            height = screenHeight;
            width = (int) (height * (intrinsicWidth / (float) intrinsicHeight));
        }
        // 设置ImageView的宽度为屏幕宽度，高度为按比例计算的值
        ViewGroup.LayoutParams params = linearLayout.getLayoutParams();
        params.width = width;
        params.height = height;
        linearLayout.setLayoutParams(params);
        linearLayout.setBackground(drawable);
    }

    private boolean requestPermissions() {
        permissionCount++;
        if (permissionCount >= 10) {
            throw new RuntimeException("权限申请失败！");
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            boolean scanPermission = PermissionChecker.checkPermission(this, android.Manifest.permission.BLUETOOTH_SCAN, android.os.Process.myPid(), android.os.Process.myUid(), getPackageName()) != PermissionChecker.PERMISSION_GRANTED;
            boolean advertisePermission = PermissionChecker.checkPermission(this, android.Manifest.permission.BLUETOOTH_ADVERTISE, android.os.Process.myPid(), android.os.Process.myUid(), getPackageName()) != PermissionChecker.PERMISSION_GRANTED;
            boolean connectPermission = PermissionChecker.checkPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT, android.os.Process.myPid(), android.os.Process.myUid(), getPackageName()) != PermissionChecker.PERMISSION_GRANTED;
            if (scanPermission || advertisePermission || connectPermission) {
                ActivityCompat.requestPermissions(this, new String[]{
                        android.Manifest.permission.BLUETOOTH_SCAN,
                        android.Manifest.permission.BLUETOOTH_ADVERTISE,
                        android.Manifest.permission.BLUETOOTH_CONNECT
                }, 1001);
                return false;
            }
        }
        boolean locationPermission = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED;
        boolean accessPermission = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED;
        if (locationPermission || accessPermission) {
            ActivityCompat.requestPermissions(this, new String[]{
                    android.Manifest.permission.ACCESS_FINE_LOCATION,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION
            }, 1002);
            return false;
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1001:
            case 1002:
                if (!requestPermissions()) {
                    return;
                }
                initView();
        }
    }
}